package wangxiaotao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import wangxiaotao.aop.annotation.Log;
import wangxiaotao.bean.vo.MenuVO;
import wangxiaotao.bean.vo.PermissionSimpleVO;
import wangxiaotao.bean.vo.PermissionVO;
import wangxiaotao.common.CommonResult;
import wangxiaotao.converter.PermissionConverter;
import wangxiaotao.entity.Permission;
import wangxiaotao.utils.TreeParserUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/admin/permission")
@RequiredArgsConstructor
public class PermissionController extends BaseController{
    private final PermissionConverter permissionConverter;

    /**
     * 获取当前用户权限下的所有权限数据  by token id
     * @return
     */
    @GetMapping("/listType")
    @Log
    public CommonResult getAllMenuByUser() {
        Subject subject = SecurityUtils.getSubject();
        //为授权
        if (!subject.isRemembered()&&!subject.isAuthenticated()){
            return CommonResult.fail(401,"请先登录",null);
        }
        Object principal = SecurityUtils.getSubject().getPrincipal();
        List<MenuVO> menuVOList = adminService.queryTypeByUserRole((String) principal);
        List<String> permissionList = menuVOList
                .stream()
                .map(MenuVO::getResourceMark)
                .collect(Collectors.toList());
        HashMap<String, Object> res = new HashMap<>();
        res.put("menuList",TreeParserUtils.getTreeList(0,true,new ArrayList<>(menuVOList)));
        res.put("permissionList",permissionList);
        return CommonResult.success(res);
    }

    /**
     * 获取所有权限 id  parent_id resource_name
     * @return
     */
    @RequiresPermissions({"permission:list"})
    @GetMapping("/list2SimpleTree")
    @Log
    public CommonResult getAllPermission2SimpleTree(){
        List<PermissionSimpleVO> permissionVOS = permissionService.
                list(new QueryWrapper<Permission>()
                        .select("id", "parent_id", "resource_name"))
                .stream().map(s -> new PermissionSimpleVO(s.getId(),s.getResourceName(),s.getParentId(),null))
                .collect(Collectors.toList());
        List<PermissionSimpleVO> treeList = TreeParserUtils.getTreeList(0,false, permissionVOS);
        return CommonResult.success(treeList);
    }

    /**
     * 获取完整的权限数据
     * @return
     */
    @RequiresPermissions({"permission:list"})
    @GetMapping("/list")
    @Log
    public CommonResult getAllPermission(){
        List<PermissionVO> permissionVOS = permissionConverter.toPermissionVOList(permissionService.list());
        return CommonResult.success(TreeParserUtils.getTreeList(0,false, permissionVOS));
    }

    /**
     * 添加权限
     * @return
     */
    @RequiresPermissions({"permission:add"})
    @PostMapping("/add")
    @Log
    public CommonResult addPermission(@Validated @RequestBody PermissionVO permissionVO){
        permissionService.save(permissionConverter.toPermission(permissionVO));
        return CommonResult.success();
    }

    /**
     * 修改权限
     * @return
     */
    @RequiresPermissions({"permission:update"})
    @PostMapping("/update")
    @Log
    public CommonResult updatePermission(@Validated @RequestBody PermissionVO permissionVO){
        permissionService.updateById(permissionConverter.toPermission(permissionVO));
        return CommonResult.success();
    }

}
